package com.tianshi.user.controller;


import com.tianshi.entity.Users;
import com.tianshi.user.service.UserService;
import com.tianshi.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    //用户登录功能
    @PostMapping("/login")
    public ResponseEntity<Users> login(String keyword, String password, HttpSession session) {
        log.info("login keyword: " + keyword + " password: " + password);
        //调用service层的登录方法
        Users user = userService.login(keyword, password);
        if(user==null){
            return ResponseEntity.badRequest().body(null); //400错误
        }else{
            Users loginUser = userService.findUserById(user.getUserId());
            session.setAttribute("loginUser", loginUser); //将用户信息存入session
            return ResponseEntity.ok(loginUser); //200成功
        }
    }

    //用户注册功能
    @PostMapping("/register")
    public ResponseEntity<String> register(@RequestBody UserVO userVo) {
        //获取用户信息
        Users user = userVo.getUsers();
        log.info("注册用户的信息："+user+" 确认密码："+ userVo.getConfirmPassword());
        //校验密码是否一致
        if(!user.getPassword().equals(userVo.getConfirmPassword())){
            return ResponseEntity.badRequest().body("error");
        }
        //调用service层的注册方法
        userService.register(user);

        return ResponseEntity.ok("success");
    }


    //验证手机号是否已注册
    @GetMapping("/checkPhone")
    public ResponseEntity<String> checkPhone(@RequestParam("phone") String phone) {
        log.info("验证手机号是否已注册："+phone);
        Users user = userService.findByPhone(phone);
        System.out.println(user);
        return user!=null? ResponseEntity.ok("该手机号已注册"):ResponseEntity.ok("该手机号未注册");
    }

    //用户信息修改功能
    @PutMapping("/updateInfo")
    public ResponseEntity<String> updateUserInfo(@RequestBody Users user, HttpSession session) {
        log.info("用户信息修改："+user);


        userService.updateUserInfo(user);
        //修改了用户的信息
        session.setAttribute("loginUser", user); //将用户信息存入session


        return ResponseEntity.ok("success");
    }
    //用户密码修改功能
    @PostMapping("/changePassword")
    public ResponseEntity<String> changePassword(Long userId, String oldPassword, String newPassword) {
        log.info("用户密码修改："+userId+" "+oldPassword+" "+newPassword);
        userService.changePassword(userId, oldPassword, newPassword);
        return ResponseEntity.ok("success");
    }

    /*
    * 退出登录
    * */
    @GetMapping("/logout")
    public ResponseEntity<String> logout(HttpSession session) {
        session.removeAttribute("loginUser");
        return ResponseEntity.ok("success");
    }

    @GetMapping("/getUserType")
    public ResponseEntity<Short> getUserType(HttpSession session) {
        log.info("getUserType获取用户id{}", session);
        Users loginUser = (Users) session.getAttribute("loginUser");
        log.info("获取用户信息{}", loginUser);
        if(loginUser==null){
            return ResponseEntity.badRequest().body(null);
        }else{
            log.info("获取用户类型{}", loginUser.getUserType());
            return ResponseEntity.ok(loginUser.getUserType());
        }
    }
}
